<template>
  <div>
    <el-row>
      <el-button
        type="primary"
        style="margin-top: 3px"
        @click="dialogFormVisible = true"
        >新增分类</el-button
      >
      <!-- <el-button type="primary" style="margin-top: 3px">查询</el-button> -->

      <el-col style="margin-top: 10px">
        <el-table
          class="box-table"
          :data="Data"
          :stripe="true"
          :border="true"
          :fit="true"
          v-if="Data != null"
        >
          <el-table-column
            prop="title"
            label="分类名称"
            width="auto"
            align="center"
            header-align="center"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="auto"
          >
            <template #default="scope">
              <el-button
                link
                type="primary"
                size="small"
                style="color: red"
                @click="delBerbak(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>

      <!-- <el-col style="margin: 0 auto">
        <el-card>
          <div class="block">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              :total="Total"
            >
            </el-pagination>
          </div>
        </el-card>
      </el-col> -->
      <el-dialog
        title="新增类别"
        v-model="dialogFormVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-form-item label="类型名称:" label-width="100px">
          <el-input v-model="Input_leibie"></el-input>
        </el-form-item>

        <div
          slot="footer"
          class="dialog-footer"
          style="margin-top: 10px; margin-left: 60%"
        >
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </div>
      </el-dialog>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, toRaw, onMounted, onBeforeMount } from "vue";
import { getAllCategories, addCategory, delCategory } from "@/api/api";
import { v4 as uuidv4 } from "uuid";
import { Delete } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { message } from "@/utils/message";
import { id } from "element-plus/es/locale";
import { useNav } from "@/layout/hooks/useNav";
defineOptions({
  // name 作为一种规范最好必须写上并且和路由的name保持一致
  name: "Liuyan"
});
const { logout } = useNav();
//表格数据
const Data = ref<any>([]);
const currentPage = ref(1); // 当前页数
const pageSize = ref(10); // 每页条数
const Total = ref<number>(1); //总条数
const reply = ref<number>(1); //当前显示

const GetList = () => {
  getAllCategories().then((res: any) => {
    console.log(res);
    if (res.code == 200) {
      Data.value = res.items;
      // Total.value = res.pagination.total;
    }
  });
};
const Input_leibie = ref<any>("");
const dialogFormVisible = ref(false);
const add = () => {
  const data = {
    title: Input_leibie.value
  };
  addCategory(data).then((res: any) => {
    console.log(res);
    if (res.code == 200) {
      message("新增成功", { type: "success" });
      dialogFormVisible.value = false;
      GetList();
    } else if (res.code == 401) {
      ElMessage({
        type: "error",
        message: "登录重新登录"
      });
      logout();
    }
  });
};

// 监听页码变化
const handleCurrentChange = val => {
  currentPage.value = val;
  console.log("未回复");
  GetList();
};

// 监听每页条数变化
const handleSizeChange = val => {
  pageSize.value = val;
};

//删除
const delBerbak = (row: any) => {
  ElMessageBox.confirm("确认删除?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      const data = {
        id: row.id
      };
      delCategory(data).then((res: any) => {
        console.log(res);
        if (res.code == 200) {
          message("删除成功", { type: "success" });
          GetList();
        } else if (res.code == 401) {
          ElMessage({
            type: "error",
            message: "需要重新登录"
          });
          logout();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除"
      });
    });
};

//关闭弹窗
const handleClose = (done: () => void) => {
  done();
};

onMounted(() => {
  GetList();
});
</script>

<style lang="scss" scoped>
.active-button {
  background-color: #409eff; /* 设置按钮激活时的背景颜色 */
  color: white; /* 设置按钮激活时的文字颜色 */
}
</style>
